package org.boyan.modules.config.b;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.boyan.common.util.SpringContextUtils;
import org.boyan.common.util.oConvertUtils;
import org.boyan.modules.config.a.b;
import org.boyan.modules.config.exception.DBException;
import org.boyan.modules.config.service.DbTableHandleI;
import org.boyan.modules.config.service.a.a;
import org.boyan.modules.config.service.a.c;
import org.boyan.modules.config.service.a.e;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @author: zhuxiaobao
 * @time: 2021/9/24 17:33
 */

public class d {
    private static final Logger b = LoggerFactory.getLogger(d.class);

    public static String a = "";

    public static org.boyan.modules.config.service.DbTableHandleI getTableHandle() throws SQLException, DBException {
        Object var0 = null;
        String var1 = org.boyan.modules.config.b.d.getDatabaseType();
        byte var3 = -1;
        switch(var1.hashCode()) {
            case -1955532418:
                if (var1.equals("ORACLE")) {
                    var3 = 2;
                }
                break;
            case -1620389036:
                if (var1.equals("POSTGRESQL")) {
                    var3 = 5;
                }
                break;
            case 2185:
                if (var1.equals("DM")) {
                    var3 = 3;
                }
                break;
            case 73844866:
                if (var1.equals("MYSQL")) {
                    var3 = 0;
                }
                break;
            case 912124529:
                if (var1.equals("SQLSERVER")) {
                    var3 = 4;
                }
                break;
            case 1557169620:
                if (var1.equals("MARIADB")) {
                    var3 = 1;
                }
        }

        switch(var3) {
            case 0:
                var0 = new org.boyan.modules.config.service.a.b();
                break;
            case 1:
                var0 = new org.boyan.modules.config.service.a.b();
                break;
            case 2:
                var0 = new c();
                break;
            case 3:
                var0 = new a();
                break;
            case 4:
                var0 = new e();
                break;
            case 5:
                var0 = new org.boyan.modules.config.service.a.d();
                break;
            default:
                var0 = new org.boyan.modules.config.service.a.b();
        }

        return (DbTableHandleI)var0;
    }

    public static Connection getConnection() throws SQLException {
        DataSource dataSource = SpringContextUtils.getApplicationContext().getBean(DataSource.class);
        return dataSource.getConnection();
    }

    public static String getDatabaseType() throws SQLException, DBException {
        if (oConvertUtils.isNotEmpty(a)) {
            return a;
        }
        DataSource dataSource = SpringContextUtils.getApplicationContext().getBean(DataSource.class);
        return a(dataSource);
    }

    public static boolean a() {
        try {
            return "ORACLE".equals(getDatabaseType());
        } catch (SQLException | DBException sQLException) {
            sQLException.printStackTrace();
        }
        return false;
    }

    public static String a(DataSource paramDataSource) throws SQLException, DBException {
        if ("".equals(a)) {
            Connection connection = paramDataSource.getConnection();
            try {
                DatabaseMetaData databaseMetaData = connection.getMetaData();
                String str = databaseMetaData.getDatabaseProductName().toLowerCase();
                if (str.contains("mysql")) {
                    a = "MYSQL";
                } else if (str.contains("oracle")) {
                    a = "ORACLE";
                } else if (str.contains("dm")) {
                    a = "DM";
                } else if (str.contains("sqlserver") || str.contains("sql server")) {
                    a = "SQLSERVER";
                } else if (str.contains("postgresql")) {
                    a = "POSTGRESQL";
                } else if (str.contains("mariadb")) {
                    a = "MARIADB";
                } else {
                    b.error("数据库类型:[" + str + "]不识别!");
                }
            } catch (Exception exception) {
                b.error(exception.getMessage(), exception);
            } finally {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
        }
        return a;
    }

    public static String a(Connection paramConnection) throws SQLException, DBException {
        if ("".equals(a)) {
            DatabaseMetaData databaseMetaData = paramConnection.getMetaData();
            String str = databaseMetaData.getDatabaseProductName().toLowerCase();
            if (str.contains("mysql")) {
                a = "MYSQL";
            } else if (str.contains("oracle")) {
                a = "ORACLE";
            } else if (str.contains("sqlserver") || str.contains("sql server")) {
                a = "SQLSERVER";
            } else if (str.contains("postgresql")) {
                a = "POSTGRESQL";
            } else if (str.contains("mariadb")) {
                a = "MARIADB";
            } else {
                b.error("数据库类型:[" + str + "]不识别!");
            }
        }
        return a;
    }

    public static String a(String paramString1, String paramString2) {
        switch (paramString2) {
            case "ORACLE":
                return paramString1.toUpperCase();
            case "POSTGRESQL":
                return paramString1.toLowerCase();
        }
        return paramString1;
    }

    public static Boolean a(String paramString) {
        Connection connection = null;
        ResultSet resultSet = null;
        try {
            String[] arrayOfString = { "TABLE" };
            connection = getConnection();
            DatabaseMetaData databaseMetaData = connection.getMetaData();
            String str1 = null;
            try {
                str1 = getDatabaseType();
            } catch (DBException dBException) {
                dBException.printStackTrace();
            }
            assert str1 != null;
            String str2 = a(paramString, str1);
            b b = SpringContextUtils.getBean(b.class);
            String str3 = b.getUsername();
            if ("ORACLE".equals(str1) || "DM".equals(str1)) {
                str3 = (str3 != null) ? str3.toUpperCase() : null;
            }
            resultSet = databaseMetaData.getTables(connection.getCatalog(), str3, str2, arrayOfString);
            if ("SQLSERVER".equals(str1)) {
                resultSet = databaseMetaData.getTables(connection.getCatalog(), null, str2, arrayOfString);
            } else {
                resultSet = databaseMetaData.getTables(connection.getCatalog(), str3, str2, arrayOfString);
            }
            if (resultSet.next()) {
                d.b.info("+ paramString + ");
                return Boolean.TRUE;
            }
            return Boolean.FALSE;
        } catch (SQLException sQLException) {
            throw new RuntimeException();
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException sQLException) {
                b.error(sQLException.getMessage(), sQLException);
            }
        }
    }

    public static Map<String, Object> a(List<Map<String, Object>> paramList) {
        HashMap<String, Object> hashMap = new HashMap<>();
        for (Map<String, Object> stringObjectMap : paramList) {
            hashMap.put(stringObjectMap.get("column_name").toString(), stringObjectMap);
        }
        return hashMap;
    }

    public static String getDialect() throws SQLException, DBException {
        String str = getDatabaseType();
        return b(str);
    }

    public static String b(String paramString) throws SQLException, DBException {
        String str = "org.hibernate.dialect.MySQL5InnoDBDialect";
        switch (paramString) {
            case "SQLSERVER":
                str = "org.hibernate.dialect.SQLServerDialect";
                break;
            case "POSTGRESQL":
                str = "org.hibernate.dialect.PostgreSQLDialect";
                break;
            case "ORACLE":
                str = "org.hibernate.dialect.OracleDialect";
                break;
            case "DM":
                str = "org.hibernate.dialect.DmDialect";
                break;
        }
        return str;
    }

    public static String c(String paramString) {
        return paramString;
    }
}
